package com.vercon.otamirror.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.vercon.commonmirror.utils.LogUtils;

/**
 * Created by csh on 2021/5/18
 * 监听电源键点击变化
 */
public class PowerKeyObserver {
    private final Context mContext;
    private IntentFilter mIntentFilter;
    private OnPowerKeyListener mOnPowerKeyListener;
    private PowerKeyBroadcastReceiver mPowerKeyBroadcastReceiver;

    public static final int ON = 1;
    public static final int OFF = 0;
    public static final int HOME = 3;//home键监听
    public static final int CLOSE = 4;//长按关机
    public static final int TOUCHPOINT = 5;//屏幕点击
    public static final int GOWAKE = 6;//点击亮屏
    public static final int GOSLEEP = 7;//点击息屏
    public static final int SHUTDOWN = 8;//点击息屏
    public static final int TOUCHPOINT_S = 9;//3288_S 点击屏幕

    private static final String ACTION_KEYCODE = "android.intent.action.keycode";
    private static final String ACTION_POWEROFF = "android.intent.action.poweroff";//定制关机广播
    private static final String ACTION_TOUCHPOINT = "android.intent.action.touchpoint";// 点击屏幕
    private static final String POWERKEYDOWN = "com.xbh.action.POWERKEYDOWN";
    private static final String ACTION_gowake = "android.intent.action.gowake";// S板卡点击亮屏
    private static final String ACTION_gosleep = "android.intent.action.gosleep";//S板卡点击息屏
    private static final String ACTION_shutdown = "android.intent.action.wk.shutdown";//S板关机
    private static final String SCREEN_CLEAR = "android.intent.action.FACTORY_RESET";//恢复出厂广播

    private static final String ACTION_TOUCHPOINT_S = "xbh.intent.action.start_ltvmanageservice";//3288_S 点击屏幕

    public PowerKeyObserver(Context context) {
        this.mContext = context;
    }

    //注册广播接收者
    public void startListen() {
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        mIntentFilter.addAction(Intent.ACTION_SCREEN_ON);
        mIntentFilter.addAction(ACTION_KEYCODE);
        mIntentFilter.addAction(ACTION_POWEROFF);
        mIntentFilter.addAction(ACTION_TOUCHPOINT);
        mIntentFilter.addAction(SCREEN_CLEAR);
        //mIntentFilter.addAction(POWERKEYDOWN);
        mIntentFilter.addAction(ACTION_gowake);
        mIntentFilter.addAction(ACTION_gosleep);
        mIntentFilter.addAction(ACTION_shutdown);
        mIntentFilter.addAction(ACTION_TOUCHPOINT_S);
        mPowerKeyBroadcastReceiver = new PowerKeyBroadcastReceiver();
        mContext.registerReceiver(mPowerKeyBroadcastReceiver, mIntentFilter);
        System.out.println("----> 开始监听");
    }

    //取消广播接收者
    public void stopListen() {
        if (mPowerKeyBroadcastReceiver != null) {
            mContext.unregisterReceiver(mPowerKeyBroadcastReceiver);
            System.out.println("----> 停止监听");
        }
    }

    // 对外暴露接口
    public void setHomeKeyListener(OnPowerKeyListener powerKeyListener) {
        mOnPowerKeyListener = powerKeyListener;
    }

    // 回调接口
    public interface OnPowerKeyListener {
        void onPowerKeyPressed(int type);
    }

    //广播接收者
    class PowerKeyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            LogUtils.i("PowerKeyObserver", intent.getAction());

            String action = intent.getAction();
            switch (action) {
                case ACTION_POWEROFF:
                    //关机
                    mOnPowerKeyListener.onPowerKeyPressed(CLOSE);
                    break;
                case Intent.ACTION_SCREEN_OFF:
                    mOnPowerKeyListener.onPowerKeyPressed(OFF);
                    break;
                case Intent.ACTION_SCREEN_ON:
                    mOnPowerKeyListener.onPowerKeyPressed(ON);
                    break;
                case ACTION_KEYCODE:
                    int keycode = intent.getIntExtra("keycode", 0);
                    boolean down = intent.getBooleanExtra("down", false);
                    LogUtils.i("PowerKeyObserver", keycode + "  " + down);
                    if (keycode == HOME && !down) {
                        //按下抬起
                        mOnPowerKeyListener.onPowerKeyPressed(HOME);
                    }
                    break;
                case ACTION_TOUCHPOINT:
                    mOnPowerKeyListener.onPowerKeyPressed(TOUCHPOINT);
                    break;
                case ACTION_gowake:
                    mOnPowerKeyListener.onPowerKeyPressed(GOWAKE);
                    break;
                case ACTION_gosleep:
                    mOnPowerKeyListener.onPowerKeyPressed(GOSLEEP);
                    break;
                case ACTION_shutdown:
                    mOnPowerKeyListener.onPowerKeyPressed(SHUTDOWN);
                    break;
                case ACTION_TOUCHPOINT_S:
                    mOnPowerKeyListener.onPowerKeyPressed(TOUCHPOINT_S);
                    break;
            }
        }
    }
} 